package com.imc.platform.base.common.controller;

import com.imc.platform.base.common.service.IdentifyCodeService;
import com.twp.common.tuple.ResultDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * Created by tanweiping on 2018/8/1.
 */
/*
* 类描述：图形验证码控制器
* 开发者：tanweiping
* 创建日期：2018/8/7 10:37
*/
@RequestMapping("/api/public/")
@RestController
public class IdentifyCodeController {
    @Autowired
    IdentifyCodeService identifyCodeService;

    /*
    * tanweiping  2018/8/7 10:37
    * 方法描述：后端生成图形验证码的位置
    */
    @GetMapping("/identify/{start}/{end}")
    public ResultDTO getIdentify(@PathVariable String start, @PathVariable String end, HttpServletRequest request){
        return ResultDTO.builder().data(
                new HashMap<String,String>(){{
                    put("code",identifyCodeService.getIdentifyCode(
                            request.getSession(),start,end
                    ));
                }}
        ).build();
    }

    /*
    * tanweiping  2018/8/7 10:38
    * 方法描述：图形验证码的位置校验
    */
    @GetMapping("/identify/verify/{code}")
    public ResultDTO verify(HttpServletRequest request,@PathVariable String code){
        return ResultDTO
                .builder()
                .isSuccess(identifyCodeService.verify(request.getSession(), code))
                .build();
    }
}
